package com.lzc.springboot.service.impl;

import com.lzc.springboot.domain.ResultInfo;
import com.lzc.springboot.domain.User;
import com.lzc.springboot.mapper.UserMapper;
import com.lzc.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserMapper userMapper;

  @Override
  public ResultInfo<User> login(User user) {
    ResultInfo<User> resultInfo = new ResultInfo<User>();
    User tempUser = userMapper.findByNameAndPwd(user);
    // 0表示登陆失败，1表示成功
    if (tempUser == null){
      resultInfo.setCode(0);
      resultInfo.setMsg("用户名或密码错误！");
    }else if ("0".equals(tempUser.getChecked())){
      resultInfo.setCode(0);
      resultInfo.setMsg("待管理员审核！请稍候");
    }else if ("2".equals(tempUser.getChecked())){
      resultInfo.setCode(0);
      resultInfo.setMsg("审核未通过，请重新注册");
    }else {
      resultInfo.setCode(1);
      resultInfo.setData(tempUser);
    }
    return resultInfo;
  }

  @Override
  public void changeUser(User user) {
    userMapper.changeUser(user);
  }

  @Override
  public User findUserById(int id) {
    return userMapper.findUserById(id);
  }

  @Override
  public void delUserById(int id) {
    userMapper.delUserById(id);
  }

  @Override
  public void expertChecked(int id) {
    userMapper.expertChecked(id);
  }

  @Override
  public void expertUnChecked(int id) {
    userMapper.expertUnChecked(id);
  }

  @Override
  public ResultInfo<User> register(User user) {
    ResultInfo<User> resultInfo = new ResultInfo<User>();
    User byUsername = userMapper.findByUsername(user.getUsername());
    if (byUsername != null){
      resultInfo.setCode(0);
      resultInfo.setMsg("用户名已经存在，请重新注册");
    }else {
      resultInfo.setCode(1);
      if ("专家".equals(user.getRole())) user.setChecked("0");
      else user.setChecked("1");
      userMapper.register(user);
      resultInfo.setData(user);
    }
    return resultInfo;
  }

}
